// pages/rent-house/index.js
import { handlerGobackClick } from "../../../utils/util";
import { Renter } from "../../../models/renter";

const renter = new Renter();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    active: [true, false],
    tabOne: [],
    loading: false,
    noMore: false,
    pageNo: 1,
    loadingFailed: false,

    tabOne0: [],
    loading0: false,
    noMore0: false,
    pageNo0: 1,
    loadingFailed0: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.initPageData();
  },
  handlerGobackClick,
  changeItme(event) {
    var index = event.currentTarget.dataset["index"];
    var active = [false, false];
    active[index] = true;
    this.setData({
      active,
      activeTab1: 0,
    });
  },
  addRent() {
    wx.navigateTo({
      url: "/owner-app/pages/add-renter/index",
    });
  },
  goRentDetail(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/owner-app/pages/rent-house-detail/index?id=" + id,
    });
  },
  initPageData() {
    renter
      .list(1, 10, 1)
      .then((res) => {
        this.setData({
          tabOne: res.result || [],
        });
      })
      .catch((err) => {
        console.log(err);
      });
    renter
      .list(1, 10, 2)
      .then((res) => {
        this.setData({
          tabOne0: res.result || [],
        });
      })
      .catch((err) => {
        console.log(err);
      });
  },
  scrollBottom() {
    if (!this.data.loading && !this.data.noMore) {
      this.setData({
        loading: true,
        pageNo: this.data.pageNo + 1,
      });
      this.getData(
        "list",
        "loading",
        "tabOne",
        "pageNo",
        "loadingFailed",
        1,
        "noMore",
        true
      );
    }
  },
  getData(
    methodName,
    loadingName,
    dataName,
    pageNoName,
    loadingFailedName,
    type = 0,
    noMoreName,
    isPage
  ) {
    renter[methodName](this.data[pageNoName], 10, type)
      .then((res) => {
        this.setData({
          [loadingName]: false,
        });
        //有分页
        if (isPage) {
          this.setData({
            [dataName]: this.data[dataName].concat(res.result),
          });
        }
        //没有分页就直接赋值
        else {
          this.setData({
            [dataName]: res.result.records,
          });
        }
        if (res.result.length == 0) {
          this.setData({
            [noMoreName]: true,
          });
        }
      })
      .catch((err) => {
        this.setData({
          [loadingFailedName]: true,
        });
      });
  },
});
